import * as echarts from '../ec-canvas/echarts'

const initRadarChart = obj =>  (canvas, width, height, dpr) => {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr
  })
  chart.showLoading()
  chart.setOption({
    backgroundColor: "#ffffff",
    xAxis: { show: false },
    yAxis: { show: false },
  })
  canvas.setChart(chart)
  obj.chartRef = chart
  return chart
}

const obj = {}

Component({
  data: {
    ec: { onInit: initRadarChart(obj) }
  },
  methods: {
    update (data) {
      obj.chartRef.setOption({
        radar: {
          indicator: data.map(_ => ({ name: _.knowledge, max: 1 }))
        },
        series: [
          {
            name: '能力',
            type: 'radar',
            data: [{ value: data.map(_ => _.rating) }]
          }
        ]
      })
      obj.chartRef.hideLoading()
    }
  }
})
